/* $Revision: eB2 sp3 $    BY: Donny Fu         DATE: 10/25/03  ECO: *@@@@@@@@* */
/*
                {gprun.i ""mbwopttr.p""
                    "(input part,
                    input qty,
                    input tsite,
                    input loc,
                    input lot,
                    input ref,
                    input to_loc,
                    input nbr,
                    input so_job,
                    output transfer_success
                    )"}
*/

define input parameter tmp_sr_part like pt_part no-undo.
define input parameter tmp_sr_comp like sr_qty no-undo. 
define input parameter tmp_sr_site like sr_site no-undo.
define input parameter tmp_sr_loc like sr_loc no-undo.
define input parameter tmp_sr_lot like sr_lotser no-undo.
define input parameter tmp_sr_ref like sr_ref no-undo.
define input parameter tmp_to_loc like sr_loc no-undo.
define input parameter tmp_nbr like qc_nbr no-undo.
define input parameter tmp_so_job like tr_so_job no-undo.
define output parameter transfer_success as integer no-undo.

/* DISPLAY TITLE */
{mfdeclre.i}
{gplabel.i}

define shared variable eff_date like tr_effdate.

define new shared variable trtype as character.
define new shared variable lotserial like sr_lotser no-undo.
define new shared variable lotserial_qty like sr_qty no-undo.
define new shared variable nbr like tr_nbr label "Order" no-undo.
define new shared variable so_job like tr_so_job no-undo.
define new shared variable rmks like tr_rmks no-undo.
define new shared variable transtype as character format "x(7)" initial "ISS-TR".
define new shared variable from_nettable like mfc_logical.
define new shared variable to_nettable like mfc_logical.
define new shared variable null_ch as character initial "".
define new shared variable old_mrpflag like pt_mrp.
define new shared variable intermediate_acct like trgl_dr_acct.
define new shared variable intermediate_sub like trgl_dr_sub.
define new shared variable intermediate_cc like trgl_dr_cc.
define new shared variable from_expire like ld_expire.
define new shared variable from_date like ld_date.
define new shared variable site_from like pt_site no-undo.
define new shared variable loc_from like pt_loc no-undo.
define new shared variable lotser_from like sr_lotser no-undo.
define new shared variable lotref_from like ld_ref no-undo.
define new shared variable status_from like ld_status no-undo.
define new shared variable site_to like pt_site no-undo.
define new shared variable loc_to like pt_loc no-undo.
define new shared variable lotser_to like sr_lotser no-undo.
define new shared variable lotref_to like ld_ref no-undo.
define new shared variable h_wiplottrace_procs as handle no-undo.
define new shared variable h_wiplottrace_funcs as handle no-undo.

define variable glcost like sct_cst_tot.
define variable undo-input like mfc_logical.
define variable yn like mfc_logical.
define variable assay like tr_assay.
define variable grade like tr_grade.
define variable expire like tr_expire.
define variable ld_recid as recid.
define variable v_abs_recid as   recid           no-undo.
define variable v_shipnbr   like tr_ship_id      no-undo.
define variable v_shipdate  like tr_ship_date    no-undo.
define variable v_invmov    like tr_ship_inv_mov no-undo.
define variable ld_recid_from as recid no-undo.
define variable lot_control like clc_lotlevel no-undo.
define variable errmsg as integer no-undo.
define variable lot_found like mfc_logical no-undo.
define variable mesg_desc as character no-undo.
define variable ve_recid as recid no-undo.
{&ICLOTR-P-TAG2}
define variable part like pt_part no-undo.
define variable trans_nbr like tr_trnbr no-undo.
define variable from-label as character no-undo.
define variable to-label as character no-undo.

define buffer lddet for ld_det.
define buffer lddet1 for ld_det.

{wlfnc.i} /* FUNCTION FORWARD DECLARATIONS */

if is_wiplottrace_enabled() then do:
   {gprunmo.i &program=""wlpl.p"" &module="AWT"
      &persistent="""persistent set h_wiplottrace_procs"""}
   {gprunmo.i &program=""wlfl.p"" &module="AWT"
      &persistent="""persistent set h_wiplottrace_funcs"""}
end.

define variable iss_trnbr like tr_trnbr no-undo.
define variable rct_trnbr like tr_trnbr no-undo.

/* CONSIGNMENT VARIABLES. */
{socnvars.i}

{gpglefdf.i}

/* SHARED TEMP TABLES */
{icshmtdf.i "new" }

/* REPLACED pt_part USING LOCAL VARIABLE part TO TRIGGER */
/* DATABASE VALIDATION                                   */
{&ICLOTR-P-TAG3}

from-label = getTermLabelRt("FROM",8).
to-label   = getTermLabelRt("TO",8).


assign  transfer_success = 0
        part = tmp_sr_part
		lotserial_qty = tmp_sr_comp
		site_from = tmp_sr_site
		loc_from = tmp_sr_loc
		lotser_from = tmp_sr_lot
		lotref_from = tmp_sr_ref
		site_to = tmp_sr_site
		loc_to = tmp_to_loc
		lotser_to = tmp_sr_lot
		lotref_to = tmp_sr_ref.

transloop:
repeat with frame a:
/*GUI*/ if global-beam-me-up then undo, leave.

   {gprun.i ""gplotwdl.p""}
/*GUI*/ if global-beam-me-up then undo, leave.

   assign
      so_job = tmp_so_job
      nbr = tmp_nbr
      rmks = "".

   find pt_mstr where pt_part = part no-lock no-error.

   for first pt_mstr fields(pt_desc1 pt_desc2 pt_loc pt_lot_ser pt_mrp pt_part pt_site pt_um) no-lock where pt_part = part:
   end.
/*GUI*/ if global-beam-me-up then undo, leave.
   
   old_mrpflag = pt_mrp.

   /* SET GLOBAL PART VARIABLE */
   global_part = pt_part.

   xferloop:
   repeat:
/*GUI*/ if global-beam-me-up then undo, leave.

      toloop:
      do for lddet on error undo, retry with frame a:
/*GUI*/ if global-beam-me-up then undo, leave.

         from-loop:
         do on error undo:
/*GUI*/ if global-beam-me-up then undo, leave.

            find si_mstr where si_site = site_from no-lock no-error.
            if not available si_mstr then do:
		    transfer_success = 1.
		    undo transloop, return.
        end.

            /* Check Site Security */
            {gprun.i ""gpsiver.p""
               "(input si_site, input recid(si_mstr), output return_int)"}
/*GUI*/ if global-beam-me-up then undo, leave.

            if return_int = 0 then do:
                transfer_success = 2.
                undo transloop, return.
            end.

            /* fdn */ {mbgpglef02.i &module = ""IC""
               &entity = si_entity
               &date   = eff_date}

            find ld_det where ld_det.ld_part = pt_part
               and ld_det.ld_site = site_from
               and ld_det.ld_loc = loc_from
               and ld_det.ld_lot = lotser_from
               and ld_det.ld_ref = lotref_from
            no-lock no-error.

            if not available ld_det then do:

               find si_mstr where si_site = site_from no-lock no-error.
               find loc_mstr where loc_site = site_from
                  and loc_loc = loc_from no-lock no-error.

               if not available si_mstr then do:
                   transfer_success = 3.
                   undo transloop, return.
               end.

               if not available loc_mstr then do:
                  if not si_auto_loc then do:
                      transfer_success = 4.
                      undo transloop, return.
                  end.
                  else do:
                     find is_mstr where is_status = si_status
                     no-lock no-error.
                     if available is_mstr and is_overissue then do:
                        create loc_mstr.
                        assign
                           loc_site = si_site
                           loc_loc = loc_from
                           loc_date = today
                           loc_perm = no
                           loc_status = si_status.
                     end.
                     else do:
                         transfer_success = 5.
                         undo transloop, return.
                     end.
                  end.
               end. /* if not available loc_mstr */

               find is_mstr where is_status = loc_status
               no-lock no-error.

               if available is_mstr and is_overissue and
                  ((pt_lot_ser <> "" and lotser_from <> "") or pt_lot_ser = "" )
               then do:
                  create ld_det.
                  assign
                     ld_det.ld_site = site_from
                     ld_det.ld_loc = loc_from
                     ld_det.ld_part = pt_part
                     ld_det.ld_lot = lotser_from
                     ld_det.ld_ref = lotref_from
                     ld_det.ld_status = loc_status
                     status_from = loc_status.
               end.
               else do:
                   transfer_success = 6.
                   undo transloop, return.
               end.

            end.

            else
            if ld_det.ld_qty_oh - lotserial_qty -
               ld_det.ld_qty_all < 0
               and ld_det.ld_qty_all > 0
               and ld_det.ld_qty_oh > 0
               and lotserial_qty > 0
            then do:
               status_from = ld_det.ld_status.
               yn = yes.
            end.

            else do:
               status_from = ld_det.ld_status.
            end.

            ld_recid_from = recid(ld_det).

            {gprun.i ""icedit.p""
               "(""ISS-TR"",
                 site_from,
                 loc_from,
                 pt_part,
                 lotser_from,
                 lotref_from,
                 lotserial_qty,
                 pt_um,
                 """",
                 """",
                 output undo-input)"}
/*GUI*/ if global-beam-me-up then undo, leave.

            if undo-input then do:
                transfer_success = 7.
                undo transloop, return.
            END.
         end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* from-loop */

         send-loop:

         do on error undo toloop, retry toloop:
/*GUI*/ if global-beam-me-up then undo, leave.

            find si_mstr where si_site = site_to no-lock no-error.
            if not available si_mstr then do:
                transfer_success = 8.
                undo transloop, return.
            end.

            {gprun.i ""gpsiver.p""
               "(input site_to, input ?, output return_int)"}
/*GUI*/ if global-beam-me-up then undo, leave.

            if return_int = 0 then do:
                transfer_success = 9.
                undo transloop, return.
            end.

            /* fdn */ {mbgpglef02.i &module = ""IC""
               &entity = si_entity
               &date   = eff_date}
               
            if (pt_lot_ser <> "") and (lotser_from <> lotser_to) then do:

               /* PERFORM COMPLIANCE CHECK  */
               {gprun.i ""gpltfnd1.p""
                  "(pt_part,
                    lotser_to,
                    """",
                    """",
                    output lot_control,
                    output lot_found,
                    output errmsg)"}
/*GUI*/ if global-beam-me-up then undo, leave.

               if ( lot_control > 0 and lot_found ) then do:
                  /* SERIAL NUMBER ALREADY EXISTS */
                   transfer_success = 10.
                   undo transloop, return.
               end.
            end.

            /* TO PREVENT DEADLOCK IN MULTIPLE SESSIONS LDDET EXCLUSIVE LOCKED */
            find lddet where lddet.ld_part = pt_part
               and lddet.ld_site = site_to
               and lddet.ld_loc  = loc_to
               and lddet.ld_lot  = lotser_to
               and lddet.ld_ref  = lotref_to
            exclusive-lock no-error.

            ld_recid = ?.
            if not available lddet then do:

               create lddet.
               assign
                  lddet.ld_site = site_to
                  lddet.ld_loc = loc_to
                  lddet.ld_part = pt_part
                  lddet.ld_lot = lotser_to
                  lddet.ld_ref = lotref_to.

               find loc_mstr where loc_site = site_to and
                  loc_loc = loc_to no-lock no-error.

               if available loc_mstr then do:
                  lddet.ld_status = loc_status.
               end.
               else do:
                  find si_mstr where si_site = site_to no-lock no-error.
                  if available si_mstr then do:
                     lddet.ld_status = si_status.
                  end.
               end.

               ld_recid = recid(lddet).

            end.

            /*ERROR CONDITIONS*/
            if  ld_det.ld_site = lddet.ld_site
               and ld_det.ld_loc  = lddet.ld_loc
               and ld_det.ld_part = lddet.ld_part
               and ld_det.ld_lot  = lddet.ld_lot
               and ld_det.ld_ref  = lddet.ld_ref
            then do:
                transfer_success = 11.
                undo transloop, return.
            end.

            if (pt_lot_ser = "S") then do:
               if lddet.ld_part = pt_part
                  and lddet.ld_site = site_to
                  and lddet.ld_loc  = loc_to
                  and lddet.ld_lot  = lotser_to
                  and lddet.ld_ref  = lotref_to
                  and lddet.ld_qty_oh > 0
               then do:
                  mesg_desc = lddet.ld_site + ', ' + lddet.ld_loc.
               end.
               else do:
                  find first lddet1 where lddet1.ld_part = pt_part
                     and lddet1.ld_lot  = lotser_to
                     and lddet1.ld_qty_oh > 0
                     and recid(lddet1) <> ld_recid_from
                  no-lock no-error.
                  if available lddet1 then do:
                     mesg_desc = lddet1.ld_site + ', ' + lddet1.ld_loc.
                  end.
               end.
            end.

            if lddet.ld_qty_oh = 0 then do:
               assign
                  lddet.ld_date  = ld_det.ld_date
                  lddet.ld_assay = ld_det.ld_assay
                  lddet.ld_grade = ld_det.ld_grade
                  lddet.ld_expire = ld_det.ld_expire.
            end.
            else do:
               /*Assay, grade or expiration conflict. Xfer not allowed*/
               if lddet.ld_grade  <> ld_det.ld_grade
                  or lddet.ld_expire <> ld_det.ld_expire
                  or lddet.ld_assay  <> ld_det.ld_assay then do:
                   transfer_success = 12.
                   undo transloop, return.
               end.
            end.

            if status_from <> lddet.ld_status then do:

            end. /*lddet.ld_status <> ld_det.ld_status*/

            find is_mstr where is_status = lddet.ld_status no-lock.
            if not is_overissue and lddet.ld_qty_oh + lotserial_qty < 0
            then do:
               /*Transfer will result in negative qty at "to" loc*/
                transfer_success = 13.
                undo transloop, return.
            end.

            {gprun.i ""icedit.p""
               "(""RCT-TR"",
                 site_to,
                 loc_to,
                 pt_part,
                 lotser_to,
                 lotref_to,
                 lotserial_qty,
                 pt_um,
                 """",
                 """",
                 output undo-input)"}
/*GUI*/ if global-beam-me-up then undo, leave.


            if undo-input and batchrun then do:
                transfer_success = 14.
                undo transloop, return.
			end.

            if undo-input then do:
                transfer_success = 15.
                undo transloop, return.
            end.

            ve_recid = recid(ld_det).

            release lddet.
            release ld_det.

            {gprun.i ""icedit.p""
               "(""ISS-TR"",
                 site_from,
                 loc_from,
                 pt_part,
                 lotser_from,
                 lotref_from,
                 lotserial_qty,
                 pt_um,
                 """",
                 """",
                 output undo-input)"}
/*GUI*/ if global-beam-me-up then undo, leave.


            if undo-input then do:
                transfer_success = 16.
                undo transloop, return.
            END.
         end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* send-loop */

         hide message.

         /* RESET GLOBAL PART VARIABLE IN CASE IT HAS BEEN CHANGED*/
         global_part = pt_part.
         global_addr = "".

         /*PASS BOTH LOTSER_FROM & LOTSER_TO IN PARAMETER LOTSERIAL*/
         lotserial = lotser_from.
         if lotser_to = "" then
            substring(lotserial,40,1) = "#".
         else
            substring(lotserial,19,18) = lotser_to.

         /* Is all information correct? */
         yn = true.
         /* Clear shipper line item temp table */
         {gprun.i  ""icshmt1c.p"" }
/*GUI*/ if global-beam-me-up then undo, leave.


         {&ICLOTR-P-TAG13}
         /* Add to shipper line item temp table */
         {gprun.i ""icshmt1a.p""
            "(pt_part,
              lotser_from,
              lotref_from,
              site_from,
              loc_from,
              lotserial_qty,
              pt_um,
              1,
              pt_net_wt * lotserial_qty,
              pt_net_wt_um,
              pt_size * lotserial_qty,
              pt_size_um)" }
/*GUI*/ if global-beam-me-up then undo, leave.


         /* Create shipper */
         {gprun.i ""icshmt.p""
            "(site_from,
              site_to,
              ""ISS-TR"",
              eff_date,
              output v_abs_recid)" }
/*GUI*/ if global-beam-me-up then undo, leave.

         /* Get associated shipper */
         find abs_mstr where recid(abs_mstr) = v_abs_recid
         no-lock no-error.
         if available abs_mstr then
         assign
            v_shipnbr  = substring(abs_id,2)
            v_shipdate = abs_shp_date
            v_invmov   = abs_inv_mov.
         else
         assign
            v_shipnbr  = ""
            v_shipdate = ?
            v_invmov   = "".

         {&ICLOTR-P-TAG15}
/*@@@@@@@@          {gprun.i ""icxfer.p""*/
/*@@@@@@@@*/          {gprun.i ""xxicxfer.p""
            "("""",
              lotserial,
              lotref_from,
              lotref_to,
              lotserial_qty,
              nbr,
              so_job,
              rmks,
              """",
              eff_date,
              site_from,
              loc_from,
              site_to,
              loc_to,
              no,
              v_shipnbr,
              v_shipdate,
              v_invmov,
              0,
              output glcost,
              output iss_trnbr,
              output rct_trnbr,
              input-output assay,
              input-output grade,
              input-output expire)"}
/*GUI*/ if global-beam-me-up then undo, leave.

      end.
/*GUI*/ if global-beam-me-up then undo, leave.
  /*end transaction toloop */

      /* CHECK TO SEE IF CONSIGNMENT IS ACTIVE */
      {gprun.i ""gpmfc01.p""
         "(input ENABLE_CUSTOMER_CONSIGNMENT,
           input 10,
           input ADG,
           input CUST_CONSIGN_CTRL_TABLE,
           output using_cust_consignment)"}
/*GUI*/ if global-beam-me-up then undo, leave.


      if using_cust_consignment then do:
         {gprunmo.i
            &program=""socncix1.p""
            &module="ACN"
            &param="""(input pt_part,
              input lotserial_qty,
              input nbr,
              input site_from,
              input loc_from,
              input lotser_from,
              input lotref_from,
              input site_to,
              input loc_to,
              input lotser_to,
              input lotref_to,
              input so_job,
              input pt_um,
              input iss_trnbr,
              input rct_trnbr)"""}
      end. /*IF USING_CUST_CONSIGNMENT*/

      /* DETERMINE IF SUPPLIER PERFORMANCE IS INSTALLED */
      if can-find (mfc_ctrl where
                  mfc_field = "enable_supplier_perf" and mfc_logical) and
         can-find (_File where _File-name = "vef_ctrl") then do:
         {gprunmo.i
            &program=""iclotrve.p""
            &module="ASP"
            &param="""(input ve_recid)"""}
      end.  /* if enable supplier performance */

      /* RECORD WIP LOT TRACING DATA WHEN CHANGING ONE LOT/SERIAL */
      /* NUMBER TO ANOTHER LOT/SERIAL NUMBER                      */
      if is_wiplottrace_enabled() and
         lotser_from <> ""        and
         lotser_to <> ""
      then do:

         run get_transaction_number in h_wiplottrace_procs
            (output trans_nbr).

         run record_lot_serial_change in h_wiplottrace_procs
            (input trans_nbr,
             input lotser_from,
             input lotser_to,
             input pt_part,
             input lotserial_qty).

      end. /* if is_wiplottrace_enabled */

      do transaction:
         if ld_recid <> ? then
            find ld_det where ld_recid = recid(ld_det) no-error.

         if available ld_det then do:
            find loc_mstr where loc_site = ld_det.ld_site
                            and loc_loc  = ld_det.ld_loc
            no-lock.
            if ld_det.ld_qty_oh = 0 and ld_det.ld_qty_all = 0 and
               not loc_perm and
               not can-find(first tag_mstr
                            where tag_site   = ld_det.ld_site
                              and tag_loc    = ld_det.ld_loc
                              and tag_part   = ld_det.ld_part
                              and tag_serial = ld_det.ld_lot
                              and tag_ref    = ld_det.ld_ref)
            then
               delete ld_det.
         end.
      end. /* end transaction */
	leave.
   end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /*repeat transaction*/
leave.
end.



if is_wiplottrace_enabled() then
   delete PROCEDURE h_wiplottrace_procs no-error.
if is_wiplottrace_enabled() then
   delete PROCEDURE h_wiplottrace_funcs no-error.
{&ICLOTR-P-TAG18}
